package com.utils;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.UUID;

public class StringUtils {
    public static String blankOrEmpty(String str) {
        if (str == null || CommonConst.BLANK.equals(str)) {
            return CommonConst.BLANK;
        } else {
            return str;
        }
    }

    public static String concatSplit(String... param) {
        String key = CommonConst.BLANK;
        int length = param.length;
        for (String k : param) {
            if (length == 1) {
                key = key + k;
            } else {
                key = key + k + CommonConst.SPLITOR;
                length--;
            }
        }
        return key;
    }

    /**
     * 如果是null返回默认值
     *
     * @param str
     * @param defStr
     * @return
     */
    public static String blankOrEmpty(String str, String defStr) {
        if (str == null || CommonConst.BLANK.equals(str)) {
            return defStr;
        } else {
            return str;
        }
    }

    public static boolean isBlankOrEmpty(String str) {
        if (str == null || CommonConst.BLANK.equals(str)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 计算地球上任意两点(经纬度)距离
     *
     * @param long1 第一点经度
     * @param lat1  第一点纬度
     * @param long2 第二点经度
     * @param lat2  第二点纬度
     * @return 返回距离 单位：米
     */
    public static Double Distance(double long1, double lat1, double long2,
                                  double lat2) {
        double a, b, R;
//        R = 6378137; // 地球半径
        R = 6371004; // 地球半径
        lat1 = lat1 * Math.PI / 180.0;
        lat2 = lat2 * Math.PI / 180.0;
        a = lat1 - lat2;
        b = (long1 - long2) * Math.PI / 180.0;
        double d;
        double sa2, sb2;
        sa2 = Math.sin(a / 2.0);
        sb2 = Math.sin(b / 2.0);
        d = 2
                * R
                * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1)
                * Math.cos(lat2) * sb2 * sb2));
        return d;
    }


    /**
     * 自增，根据指定位数补位
     *
     * @param length
     * @param num
     * @return
     */
    public static String incrLen(int length, String num) {
        StringBuilder res = new StringBuilder(0);
        res.append(num);
        StringBuilder tmp = new StringBuilder();
        if (res.length() < length) {
            for (int i = 0; i < length - res.length(); i++) {
                tmp.append('0');
            }
        }
        String result = tmp.toString() + res.toString();
        return result;
    }


    /**
     * 科学计数法转string
     *
     * @param notation
     * @return
     */
    public static String scientToString(String notation) {
        if (StringUtils.isBlankOrEmpty(notation)) {
            return CommonConst.BLANK;
        }
        BigDecimal bigDecimal = new BigDecimal(notation);
        return bigDecimal.setScale(10, BigDecimal.ROUND_HALF_DOWN).toPlainString();
    }

    public static Long scientToLong(String notation) {
        BigDecimal bigDecimal = new BigDecimal(notation);
        return bigDecimal.longValue();
    }

    /**
     * 转换成分为单位
     *
     * @param amount 元为单位的
     * @return 分为单位的
     */
    public static String convertToFen(String amount) {
        return new BigDecimal(amount).multiply(new BigDecimal(100)).toBigInteger().toString();
    }


    /**
     * 按20170711的新需求格式化距离
     *
     * @param distance
     * @return
     */
    public static String formatDistance(String distance) {
        if (StringUtils.isBlankOrEmpty(distance)) {
            return "火星";
        }
        double temp = Double.valueOf(distance);
        if (temp < 0.01) {
            return "0.01km";
        } else if (temp < 10) {
            return distance + "km";
        } else if (temp < 100) {
            return (int) temp + "km";
        } else {
            return "99+km";
        }
    }

    public static String formatNumber(String num) {
        Long tmp = Long.valueOf(num);
        if (tmp < 10000) {
            return tmp.toString();
        } else {
            BigDecimal res = new BigDecimal(num).divide(new BigDecimal("10000"));

            return res.setScale(1, BigDecimal.ROUND_DOWN).toString() + "万";
        }
    }

    public static void main(String[] args) {
        System.out.println(formatNumber("14562"));
    }
}
